//
// Created by Jack Pei on 15/8/9.
// Copyright (c) 2015 ourslook. All rights reserved.
//

#import "UIViewController+common.h"
#import "NSString+Extension.h"
#import "UIView+Extension.h"
#import "Macro.h"
#import "UIColor+Extension.h"


@implementation UIViewController (common)


// 返回按钮
- (void)backButtonClick:(UIButton *)sender {
    // 关闭当前的ViewController
    UIViewController *vc = self.navigationController.topViewController;
    if (vc != nil && [vc isKindOfClass:self.class]) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self dismissViewControllerAnimated:YES completion:^{
            NSLog(@"模态方式关闭的");
        }];
    }
}

- (void)rightTitleClick:(UIButton *)sender {
    NSLog(@"右侧的titl被点击 了...");
}


//定义导航视图
void NAV_INIT(UIViewController *vc ,NSString *title, NSString *left ,SEL leftObj, NSString *right, SEL rightObj)
{
    
    if(left != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 8, 30, 25)];
        UIImageView *leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];// 25,25
        leftImgView.image = [UIImage imageNamed:@"return"];
        leftImgView.contentMode = UIViewContentModeScaleAspectFit;

        [button addSubview:leftImgView];
        [button addTarget:vc.self action:leftObj forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.leftBarButtonItem = leftBarBtnItem;
    }


    //Ios7 在自定义leftBarButtonItem情况下的右滑返回问题
    if (vc) {
        vc.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)vc;
    }

    if(right != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 10, 30, 25)];
        if ([right isEqual:@"编辑"] ||
            [right isEqual:@"完成"] ) {
            [button setTitle:right forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [[button titleLabel] setTextAlignment:NSTextAlignmentRight];
            if (right.length >= 3) {
                button.width = 60;
            }
            [[button titleLabel] setFont:kFontB(30)];
        }else{
            UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
            rightImgView.image = [UIImage imageNamed:right];
            rightImgView.contentMode = UIViewContentModeScaleAspectFit;
            [button addSubview:rightImgView];
        }

        [button addTarget:vc.self action:rightObj forControlEvents:UIControlEventTouchUpInside];

        [button setBackgroundColor:[UIColor clearColor]];
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.rightBarButtonItem = rightBtnItem;
    }

    if(title != nil){
        UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(110, 6, 100, 32)];
        titleView.text = title;
        [titleView setFont:[UIFont systemFontOfSize:20.0f]];
        [titleView setTextColor:[UIColor whiteColor]];
        titleView.textAlignment = NSTextAlignmentCenter;
        [titleView setBackgroundColor:[UIColor clearColor]];
        [titleView setUserInteractionEnabled:YES];
        vc.navigationItem.titleView = titleView;
        // 添加事件关闭键盘
        UITapGestureRecognizer *tapGuest = [[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(tapAnyWhereDismissKeyboard:)];
        [titleView addGestureRecognizer:tapGuest];
    }

}





//定义导航视图
void NAV_IMGINIT(UIViewController *vc ,NSString *title, NSString *left ,SEL leftObj, NSString *right, SEL rightObj)
{
    
    if(left != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 8, 30, 25)];
        UIImageView *leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
        leftImgView.contentMode = UIViewContentModeScaleAspectFit;
        leftImgView.image = [UIImage imageNamed:left];
        [button addSubview:leftImgView];
        [button addTarget:vc.self action:leftObj forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.leftBarButtonItem = leftBarBtnItem;
    }
    
    //Ios7 在自定义leftBarButtonItem情况下的右滑返回问题
    if (vc) {
        vc.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)vc;
    }
    
    
    if(right != nil){
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(220, 10, 30, 25)];
        if ([right isEqual:@"添加"]) {
            [button setTitle:right forState:UIControlStateNormal];
            [button setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
            [[button titleLabel] setFont:kFontB(30)];
        }else{
            UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
            rightImgView.image = [UIImage imageNamed:right];
            rightImgView.contentMode = UIViewContentModeScaleAspectFit;
            [button addSubview:rightImgView];
        }
        
        [button addTarget:vc.self action:rightObj forControlEvents:UIControlEventTouchUpInside];
        
        [button setBackgroundColor:[UIColor clearColor]];
        UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        vc.navigationItem.rightBarButtonItem = rightBtnItem;
    }
    
    if(title != nil){
        if ([title isEqual:@"首页"]) {
            UIImageView *titleImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_biaoti"]];
            [titleImgV sizeToFit];
            vc.navigationItem.titleView = titleImgV;
            return;
        }
        UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(110, 8, 100, 17)];
        titleView.text = title;
        [titleView setFont:[UIFont systemFontOfSize:20.0f]];
        [titleView setTextColor:[UIColor blackColor]];
        titleView.textAlignment = NSTextAlignmentCenter;
        [titleView setBackgroundColor:[UIColor clearColor]];
        vc.navigationItem.titleView = titleView;
        
        // 添加事件关闭键盘
        [titleView setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapGuest = [[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(tapAnyWhereDismissKeyboard:)];
        [vc.navigationItem.titleView addGestureRecognizer:tapGuest];
    }
    
}




- (void)tapAnyWhereDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //将self.view所有的subview的first responder 都resign
    [self.view endEditing:YES];
}
@end


@implementation TitleView
@end